package com.cz.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.ssyx.acl.mapper.RoleMapper;
import com.cz.ssyx.acl.service.AdminRoleService;
import com.cz.ssyx.acl.service.PermissionService;
import com.cz.ssyx.acl.service.RolePermissionService;
import com.cz.ssyx.acl.service.RoleService;
import com.cz.ssyx.model.acl.AdminRole;
import com.cz.ssyx.model.acl.Permission;
import com.cz.ssyx.model.acl.Role;
import com.cz.ssyx.model.acl.RolePermission;
import com.cz.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/14 20:43
 * @version: 1.0
 */


@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;


    //1. 查询角色列表（分页）
    @Override
    public IPage<Role> selectRolePage(Page<Role> rolePage, RoleQueryVo roleQueryVo) {
        //1.获取条件值
        String roleName = roleQueryVo.getRoleName();

        //2.构建表达式
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();

        //3.判断roleName是否为空
        if (!StringUtils.isEmpty(rolePage)) {
            queryWrapper.like(Role::getRoleName, roleName);
        }

        Page<Role> page = baseMapper.selectPage(rolePage, queryWrapper);

        return page;
    }

    //2.查询所有角色和该用户的所有角色
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //1.查询出所有角色
        List<Role> allRolesList = this.list();
        // allRoleList = roles
        // List<Role> roles = baseMapper.selectList(null);

        //2.根据adminId查询该用户的所有角色
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(adminId != null, AdminRole::getAdminId, adminId);

        //这里得到的是用户与角色的关系对象
        List<AdminRole> adminRoleList = adminRoleService.list(queryWrapper);

        //3.要遍历出adminRoleList中的角色id
        List<Long> roleList = adminRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());

        //4.遍历所有角色列表，找出该用户的角色封装到一个list
        ArrayList<Role> adminRoles = new ArrayList<>();

        for (Role role : allRolesList) {
            if (roleList.contains(role.getId())){
                adminRoles.add(role);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("allRolesList", allRolesList);
        map.put("assignRoles", adminRoles);
        return map;
    }

    //3.为用户分配角色
    @Override
    public void saveAdminRole(Long adminId, List<Long> roleId) {

        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getAdminId, adminId);

        //1.先清除该admin下的所有角色
        adminRoleService.remove(queryWrapper);

        //2.再重新添加所有角色
//        for (Long roleID : roleId) {
//            AdminRole adminRole = new AdminRole();
//            adminRole.setAdminId(adminId);
//            adminRole.setRoleId(roleID);
//            adminRoleService.save(adminRole);
//        }
        ArrayList<AdminRole> adminRoles = new ArrayList<>();
        for (Long roleID : roleId) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleID);
            adminRoles.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoles);
    }



}
